<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
    <div id="app">
        <p>ask a yes or no question</p>
        <input v-model='question' />
        <p>{{answer}}</p>
    </div>

    <script>
        const app={
            data() {
                return {
                    question:'',
                    answer:'questions usually contain a question mark. ;-)'
                }
            },
            watch:{
                question(newVal,oldVal){
                    if(newVal.indexOf('?')>-1){
                        this.getAnswer()
                    }
                }
            },
            methods: {
                getAnswer(){
                    this.answer = 'Thinking...'
                    axios.get('https://yesno.wtf/api')
                    .then(response=>{
                        this.answer=response.data.answer
                    })
                    .catch(error=>{
                        this.answer = 'Error! Could not reach the API. ' + error
                    })
                }
            },
        }
        Vue.createApp(app).mount("#app")

    </script>
</body>
</html>